<!-- TODO -->
<template>
	<div>
		<el-card class="!border-none" shadow="never">
			<el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
				<el-form-item label="视频名称">
					<el-input
							class="w-[280px]"
							v-model="queryParams.title"
							placeholder="请输入视频名称"
							clearable
							@keyup.enter="resetPage"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none mt-4" shadow="never">
			<el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd">
				添加视频
			</el-button>
			<!--			<el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd">-->
			<!--				一键删除-->
			<!--			</el-button>-->
			<el-table size="large" :data="pager.lists"   @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" />
				<el-table-column label="ID" prop="id" min-width="100">
					<template #default="{ row }">
						{{ row.id }}
					</template>
				</el-table-column>
				<el-table-column label="名称" prop="title" min-width="120" >
					<template #default="{ row }">
						{{ row.title }}
					</template>
				</el-table-column>
				<el-table-column label="封面图" prop="image" min-width="120" >
					<template #default="{ row }">
						<el-avatar
								:src="row.image || row.goods_image"
								style="height: 60px; min-width: 60px"
						/>
					</template>
				</el-table-column>
<!--				<el-table-column label="修改时间" prop="created_at" min-width="180" />-->
<!--				<el-table-column label="创建时间" prop="created_at" min-width="180" />-->
				<el-table-column label="操作" fixed="right" min-width="160">
					<template #default="{ row }">
						<el-button type="primary" @click="handleEdit(row)"> 编辑 </el-button>
						<el-button type="danger" @click="handleDelete(row.id)">
							删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="flex justify-end mt-4">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup>
	import { usePaging } from '@/hooks/usePaging'
	import { videoList, decVideo} from '@/api/reception'
	import EditPopup from './edit.vue'
	import feedback from '@/utils/feedback'
	import {ref} from "vue";

	const queryParams = reactive({
		title: '',
	});

	const { pager, getLists, resetPage, resetParams } = usePaging({
		fetchFun: videoList,
		params: queryParams
	})
	getLists()


	//删除
	const handleDelete = async (id : any) => {
		await feedback.confirm('确定要删除该视频？')
		const ddd = await decVideo({ video_id: id})
		getLists()
	}

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	const showEdit = ref(false)
	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	const handleEdit = async (row:any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.getDetail(row)
	}


	// 查询
	const newResetPage = async () => {
		resetPage()
	};
</script>
<style lang="scss" scope>
	//TODO
	.el-alert--warning.is-light {
		background-color: rgba(246, 248, 255, 1);
		color: rgba(64, 115, 250, 1);
	}
</style>
